/*
 * @Author: Warren
 * @Date: 2025-04-14 21:21:26
 * @LastEditors: Warren
 * @LastEditTime: 2025-07-03 22:03:15
 * @FilePath: /nest-web/src/modules/auth/auth.service.ts
 * @Description: 请填写文件说明
 */
import { Injectable } from '@nestjs/common';
import { LoginAuthDto } from './dto/login-auth.dto';
import { UserService } from '../user/user.service';
import { JwtService } from '@nestjs/jwt';
// import { CreateAuthDto } from './dto/create-auth.dto';
// import { UpdateAuthDto } from './dto/update-auth.dto';

@Injectable()
export class AuthService {
    constructor(
        private readonly userService: UserService,
        private readonly jwtService: JwtService
    ){}

    
    async login(loginRequest: LoginAuthDto): Promise<string> {
        /* 查找用户 */
        const user = await this.userService.findOne(loginRequest.username)

        if (user) {
            const validatePwd = await user.comparePassword(loginRequest.password)

            if (validatePwd) {
                const payload = {
                    sub: user.id,
                    username: user.username,
                }

                const token = await this.jwtService.signAsync(payload)

                return token
            } else { 
                return '密码错误'
            }
        } else {
            return '没有当前用户'
        }
    }

    // create(createAuthDto: CreateAuthDto) {
    //     return 'This action adds a new auth';
    // }

    // findAll() {
    //     return 'This action returns all auth';
    // }

    // findOne(id: number) {
    //     return `This action returns a #${id} auth`;
    // }

    // update(id: number, updateAuthDto: UpdateAuthDto) {
    //     return `This action updates a #${id} auth`;
    // }

    // remove(id: number) {
    //     return `This action removes a #${id} auth`;
    // }
}
